package cnm.red5;

import org.red5.server.adapter.MultiThreadedApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.service.IServiceCapableConnection;
import org.red5.server.api.IScope;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.UUID;
import java.lang.Integer;

import cnm.model.*;
import cnm.dao.*;

public class Application extends MultiThreadedApplicationAdapter {

	private ColumnDAO columnDAO;
	private ProgramDAO programDAO;
	private UserDAO userDAO;

	public void setColumnDAO(ColumnDAO columnDAO)
	{
		this.columnDAO = columnDAO;
	}
	
	public void setProgramDAO(ProgramDAO programDAO)
	{
		this.programDAO = programDAO;
	}
	
	public void setUserDAO(UserDAO userDAO)
	{
		this.userDAO = userDAO;
	}

	@Override
	public boolean appStart(IScope app) {
		//codecService.addListener(this);
		return super.appStart(app);
	}

	@Override
	public void appStop(IScope app) {
		//codecService.removeListener(this);
		super.appStop(app);
	}

	/**
	 * Return the columnList.
	 * 
	 * @param params
	 * 			map including keys: "index", "length"
	 * @return 0 - column exist, 304 - column not exist
	 * 			result ColumnList	
	 * @throws ServiceException
	 */
	public Result getAllColumns(IConnection con, Map<String, Object> params) throws Exception
	{
		try {
			List<Column> list = columnDAO.findAll(
					Integer.parseInt(params.get("index").toString()),
					Integer.parseInt(params.get("length").toString()), 
					null
					);
			return new Result(list.toArray());
		}
		catch (Exception e)
		{			
			e.printStackTrace();
		}		
		return new Result(ErrorType.SYSTEM_ERROR);
	}
	
	public Result getColumnCount(IConnection con, Map<String, Object> params) throws Exception
	{
		try {
			Long count = columnDAO.getCount();
			return new Result(count);
		}
		catch (Exception e)
		{			
			e.printStackTrace();
		}		
		return new Result(ErrorType.SYSTEM_ERROR);
	}
	
	public Result updateColumn(IConnection con, Map<String, Object> params) throws Exception
	{
		try {
			int columnId = Integer.parseInt((String) params.get("id"));
			String columnName = (String) params.get("name");
			String columnUuid = (String) params.get("uuid");
			String columnDesc = (String) params.get("desc");
			int columnType = Integer.parseInt((String) params.get("type"));			
			Column column = new Column(); //(Column) columnDAO.findById(columnId); 
			column.setColumnId(columnId);
			column.setColumnName(columnName);
			column.setColumnUuid(columnUuid);
			column.setColumnType(columnType);
			column.setColumnDesc(columnDesc);
			columnDAO.update(column);
			return new Result(ErrorType.NO_ERROR);
		}
		catch (Exception e)
		{			
			e.printStackTrace();
		}		
		return new Result(ErrorType.SYSTEM_ERROR);
	}
		
	@SuppressWarnings("unchecked")
	public Result searchProgram(IConnection con, Map<String, Object> params) throws Exception
	{
		try {
			String keyword = (String) params.get("keyword");
			int index = Integer.parseInt(params.get("index").toString());
			int length = Integer.parseInt(params.get("length").toString());
			List<Program> list;
			if (keyword.equals("")) {
				Sorter sorter = Sorter.desc("programId");
				list = programDAO.findAll(index, length, sorter);
			} else {
				list = programDAO.findByKeyword(keyword, index, length);
			}

			ArrayList piList = new ArrayList();
			for (int i = 0; i<list.size(); i++) {
				ProgramInfo pi = new ProgramInfo();
				int columnId = list.get(i).getColumn().getColumnId();
				pi.setColumnId(columnId);
				//Column column = columnDAO.findById(columnId);
				//pi.setColumnName(column.getColumnName());
				pi.setProgramName(list.get(i).getProgramName());
				pi.setProgramDate(list.get(i).getProgramDate());
				pi.setProgramDesc(list.get(i).getProgramDesc());
				pi.setProgramDj(list.get(i).getProgramDj());
				pi.setProgramId(list.get(i).getProgramId());
				pi.setProgramUuid(list.get(i).getProgramUuid());
				piList.add(pi);
			}
			return new Result(piList.toArray());
		}
		catch (Exception e)
		{			
			e.printStackTrace();
		}		
		return new Result(ErrorType.SYSTEM_ERROR);
	}
		
	public Result getProgramCount(IConnection con, Map<String, Object> params) throws Exception
	{
		try {
			String keyword = (String) params.get("keyword");
			Long count;
			if (keyword.equals("")) {
				count = programDAO.getCount();
			} else {
				count = programDAO.getCountByKeyword(keyword);
			}
			return new Result(count);
		}
		catch (Exception e)
		{			
			e.printStackTrace();
		}		
		return new Result(ErrorType.SYSTEM_ERROR);
	}
	
}
